package google.sites.model.commands.site;

import com.google.gdata.data.PlainTextConstruct;
import com.google.gdata.data.sites.SiteEntry;
import com.google.gdata.data.sites.Theme;
import com.google.gdata.util.ServiceException;
import dev.tools.design.patterns.command.CommandException;
import google.sites.model.commands.SitesCommand;

import java.io.IOException;
import java.net.URL;

/**
 * Created with IntelliJ IDEA.
 * User: Nuno
 * Date: 03-10-2013
 * Time: 19:49
 */
public class CreateSite extends SitesCommand {
    private String title;
    private String summary;
    private String theme;

    @Override
    protected void protectedExecute() throws CommandException {
        try {

                SiteEntry entry = new SiteEntry();
                entry.setTitle(new PlainTextConstruct(title));
                entry.setSummary(new PlainTextConstruct(summary));

                if (theme != null) {
                    Theme tt = new Theme();
                    tt.setValue(theme);
                    entry.setTheme(tt);
                }

                setResult(getService().insert(new URL(getSiteFeedUrl()), entry));

        } catch (IOException | ServiceException e) {
            throw new CommandException(e);
        }
    }

    public CreateSite setTitle(String title) {
        this.title = title;
        return this;
    }

    public CreateSite setSummary(String summary) {
        this.summary = summary;
        return this;
    }

    public void setTheme(String theme) {
        this.theme = theme;
    }
}
